package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.cj.pdos.entity.*;
import pers.cj.pdos.mapper.*;
import pers.cj.pdos.service.IBorrowOutBackService;
import pers.cj.pdos.utils.PdosUtils;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.utils.UuidUtils;
import pers.cj.pdos.vo.BorrowInBackGoodsVo;
import pers.cj.pdos.vo.BorrowInBackVo;
import pers.cj.pdos.vo.BorrowOutBackGoodsVo;
import pers.cj.pdos.vo.BorrowOutBackVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2020-03-25
 */
@Service
public class BorrowOutBackServiceImpl extends ServiceImpl<BorrowOutBackMapper, BorrowOutBack> implements IBorrowOutBackService {
    @Autowired
    BorrowOutBackMapper borrowOutBackMapper;
    @Autowired
    BorrowOutBackGoodsMapper borrowOutBackGoodsMapper;
    @Autowired
    GoodsSnMapper goodsSnMapper;
    @Autowired
    GoodsSnLogMapper goodsSnLogMapper;
    @Autowired
    GoodsStockMapper goodsStockMapper;
    @Autowired
    HouseWaitInMapper houseWaitInMapper;
    @Autowired
    HouseWaitInGoodsMapper houseWaitInGoodsMapper;
    @Autowired
    BorrowOutMapper borrowOutMapper;
    @Autowired
    BorrowOutGoodsMapper borrowOutGoodsMapper;

    @Override
    public boolean checkNo(String no, String companyId) {
        return borrowOutBackMapper.selectCount(new QueryWrapper<BorrowOutBack>().eq("company_id",companyId).eq("no",no)) > 0;
    }

    @Override
    public void insert(BusinessSet businessSet, BorrowOutBack borrowOutBack, List<BorrowOutBackGoods> borrowOutBackGoodsList, List<GoodsSn> goodsSnList, List<GoodsSnLog> goodsSnLogList) {
        borrowOutBackMapper.insert(borrowOutBack);
        borrowOutBackGoodsMapper.batchInsert(borrowOutBackGoodsList);

        BorrowOutBackVo borrowOutBackVo = borrowOutBackMapper.getVoById(borrowOutBack.getId());

        processBorrowOut(borrowOutBackVo);

        if (!PdosUtils.houseInOut(businessSet)) {
            //未启用出入库管理，直接添加库存
            processGoodsStock(borrowOutBackVo);
            if (!Utils.isEmpty(goodsSnList)) {
                goodsSnMapper.insertBatch(goodsSnList);
                goodsSnLogMapper.insertBatch(goodsSnLogList);
            }
        } else {
            //启用库存管理，则添加待出库单数据
            processHouseWaitIn(borrowOutBackVo);
        }
    }

    @Override
    public BorrowOutBackVo getVoById(String id) {
        return borrowOutBackMapper.getVoById(id);
    }

    @Override
    public List<BorrowOutBackVo> listVoByBorrowOutId(String borrowOutId) {
        return borrowOutBackMapper.listVoByBorrowOutId(borrowOutId);
    }

    private void processBorrowOut(BorrowOutBackVo borrowOutBackVo){
        List<BorrowOutGoods> borrowOutGoodsList = new ArrayList<>();
        for (BorrowOutBackGoodsVo vo :borrowOutBackVo.getBorrowOutBackGoodsVoList()){
            BorrowOutGoods entity = new BorrowOutGoods();
            entity.setId(vo.getBorrowOutGoodsId());
            entity.setBorrowOutId(borrowOutBackVo.getBorrowOutId());
            entity.setGoodsId(vo.getGoodsId());
            entity.setNumber(vo.getNumber());
            entity.setHasNumber(vo.getNumber());
            entity.setSaleNumber(new BigDecimal(0));
            entity.setWaitNumber(vo.getNumber().negate());
            entity.setUnitId(vo.getUnitId());
            entity.setTimes(vo.getTimes());
            entity.setPrice(new BigDecimal(0));
            entity.setAmount(new BigDecimal(0));
            entity.setCompanyId(borrowOutBackVo.getCompanyId());
            borrowOutGoodsList.add(entity);
        }
        borrowOutGoodsMapper.insertBatch(borrowOutGoodsList);

        BorrowOut temp = new BorrowOut();
        temp.setId(borrowOutBackVo.getBorrowOutId());

        long totalWaitInNumber = borrowOutGoodsMapper.countWaitNumber(borrowOutBackVo.getBorrowOutId());
        long number = borrowOutGoodsMapper.countNumber(borrowOutBackVo.getBorrowOutId());
        if(totalWaitInNumber > 0) {
            if (number == totalWaitInNumber){//未归还
                temp.setOutStatus(1);
            } else {//部分归还
                temp.setOutStatus(2);
            }
        } else {
            //全部归还
            temp.setOutStatus(3);
        }
        borrowOutMapper.updateById(temp);
    }

    /**
     * 库存处理
     * @param borrowOutBackVo
     */
    private void processGoodsStock(BorrowOutBackVo borrowOutBackVo){
        List<GoodsStock> goodsStockList = new ArrayList<>();
        for (BorrowOutBackGoodsVo vo : borrowOutBackVo.getBorrowOutBackGoodsVoList()) {
            BigDecimal number = vo.getNumber().multiply(vo.getTimes());

            //库存
            GoodsStock goodsStock = new GoodsStock();
            goodsStock.setId(UuidUtils.getUUID());
            goodsStock.setGoodsId(vo.getGoodsId());
            goodsStock.setHouseId(borrowOutBackVo.getHouseId());
            goodsStock.setNum(number); //数量*换算倍数
            goodsStock.setTime(new Date());
            goodsStock.setCompanyId(borrowOutBackVo.getCompanyId());
            goodsStockList.add(goodsStock);
        }

        goodsStockMapper.insertBatch(goodsStockList);
    }

    private void processHouseWaitIn(BorrowOutBackVo borrowOutBackVo) {
        HouseWaitIn houseWaitIn = new HouseWaitIn();
        houseWaitIn.setId(UuidUtils.getUUID());
        houseWaitIn.setBorrowOutBackId(borrowOutBackVo.getId());
        houseWaitIn.setHouseId(borrowOutBackVo.getHouseId());
        houseWaitIn.setClose(false);
        houseWaitIn.setStatus(1);//待出库
        houseWaitIn.setType(5);//设置类型为借出归还
        houseWaitIn.setCompanyId(borrowOutBackVo.getCompanyId());
        houseWaitIn.setTime(borrowOutBackVo.getTime());
        houseWaitInMapper.insert(houseWaitIn);

        List<HouseWaitInGoods> houseWaitInGoodsList = new ArrayList<>();
        for (BorrowOutBackGoodsVo vo : borrowOutBackVo.getBorrowOutBackGoodsVoList()) {
            HouseWaitInGoods houseWaitInGoods = new HouseWaitInGoods();
            houseWaitInGoods.setId(UuidUtils.getUUID());
            houseWaitInGoods.setHouseWaitInId(houseWaitIn.getId());
            houseWaitInGoods.setBorrowOutBackGoodsId(vo.getId());
            houseWaitInGoods.setGoodsId(vo.getGoodsId());
            houseWaitInGoods.setHasInNumber(new BigDecimal(0));
            houseWaitInGoods.setWaitInNumber(vo.getNumber());
            houseWaitInGoods.setNumber(vo.getNumber());
            houseWaitInGoods.setUnitId(vo.getUnitId());
            houseWaitInGoods.setTimes(vo.getTimes());
            houseWaitInGoods.setCompanyId(borrowOutBackVo.getCompanyId());
            houseWaitInGoodsList.add(houseWaitInGoods);
        }
        houseWaitInGoodsMapper.insertBatch(houseWaitInGoodsList);
    }
}
